﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FrameworkLib.Configurations.FormsConfiguration.BL;
using FrameworkLib.Configurations.FormsConfiguration;
using BrightIdeasSoftware;

namespace FrameworkLib.Configurations.FormsConfiguration
{
    public static partial class FormConfigurationDecorator
    {
        public const string CONFIGURAZIONE_MASCHERA_MENUITEM = "ConfigurazioneMaschera";
        public const string SEPARATOR_MENUITEM = "Separator";

        static private SavingConfigurationBag m_savingConfigurationBag = null;
        static private ObjectListView m_grid = null;
        static private Form m_form = null;

        private static ObjectListView Grid
        {
            get { return m_grid; }
            set { m_grid = value; }
        }

        public static SavingConfigurationBag SavingConfigurationBag
        {
            get { return m_savingConfigurationBag; }
            set { m_savingConfigurationBag = value; }
        }

        private static void AddCnfgmenu()
        {
            ToolStripMenuItem tsmiSC = new ToolStripMenuItem("Salva Configurazione");
            tsmiSC.Name = "SalvaConfigurazione";
            tsmiSC.Click += new EventHandler(tsmiSC_Click);
            tsmiSC.MergeAction = MergeAction.Append;

            ToolStripMenuItem tsmiRCD = new ToolStripMenuItem("Ripristina Configurazione di default");
            tsmiRCD.Name = "RipristinaConfigurazioneDefault";
            tsmiRCD.Click += new EventHandler(tsmiRCD_Click);
            tsmiRCD.MergeAction = MergeAction.Append;

            ToolStripSeparator tsmiSM = new ToolStripSeparator();
            tsmiSM.Name = SEPARATOR_MENUITEM;
            tsmiSM.MergeAction = MergeAction.Append;

            ToolStripMenuItem tsmiCM = new ToolStripMenuItem("Configurazione Maschera");
            tsmiCM.Name = CONFIGURAZIONE_MASCHERA_MENUITEM;
            tsmiCM.MergeAction = MergeAction.Append;
            tsmiCM.DropDownItems.Add(tsmiSC);
            tsmiCM.DropDownItems.Add(tsmiRCD);

            ToolStripMenuItem tsmi = new ToolStripMenuItem("Configurazione");
            tsmi.MergeAction = MergeAction.MatchOnly;
            tsmi.DropDownItems.Add(tsmiSM);
            tsmi.DropDownItems.Add(tsmiCM);

            MenuStrip ms = new MenuStrip();
            ms.Visible = false;
            ms.Items.Add(tsmi);
            ms.AllowMerge = true;

            m_form.Controls.Add(ms);

            ToolStripManager.RevertMerge(m_form.MdiParent.MainMenuStrip);
            ToolStripManager.Merge(ms, m_form.MdiParent.MainMenuStrip);
        }

        static void tsmiRCD_Click(object sender, EventArgs e)
        {
            OnRipristinaConfigurazioneDefaultMaschera();
        }

        static void tsmiSC_Click(object sender, EventArgs e)
        {
            OnSalvaConfigurazioneMaschera();
        }

        public static void Initialize(Form form, ObjectListView grid)
        {
            m_form = form;
            m_grid = grid;

            System.Security.Principal.IPrincipal m_operatore = System.Threading.Thread.CurrentPrincipal;
            string[] roles = System.Web.Security.Roles.GetRolesForUser(m_operatore.Identity.Name);

            SavingConfigurationBag scb = new SavingConfigurationBag();
            scb.FormName = m_form.Name;
            scb.RoleName = roles[0];
            SavingConfigurationBag = scb;

            AddCnfgmenu();

            LoadConfigurazioneMaschera();
        }

        private static void LoadConfigurazioneMaschera()
        {
            ColumnsConfigurationManager ccm = new ColumnsConfigurationManager();
            byte[] state = ccm.GetState(SavingConfigurationBag);

            if (state != null)
            {
                bool retValue = m_grid.RestoreState(state);
            }
        }

        private static void OnSalvaConfigurazioneMaschera()
        {
            byte[] data = m_grid.SaveState();

            System.Security.Principal.IPrincipal m_operatore = System.Threading.Thread.CurrentPrincipal;
            string[] roles = System.Web.Security.Roles.GetRolesForUser(m_operatore.Identity.Name);

            SavingConfigurationBag scb = new SavingConfigurationBag();
            scb.FormName = m_form.Name;
            scb.RoleName = roles[0];

            ColumnsConfigurationManager ccm = new ColumnsConfigurationManager();
            ccm.Configuration = scb;
            ccm.State = data;
            ccm.SaveState();
        }

        private static void OnRipristinaConfigurazioneDefaultMaschera()
        {
            foreach (OLVColumn c in m_grid.AllColumns)
            {
                c.IsVisible = true;
            }

            m_grid.RebuildColumns();
        }
    }
}
